﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.DomainModel.Repositories;
using Enterprise.Framework.Core.Exceptions;
using Enterprise.Framework.DomainModel.Specifications;
using Resto.CostControl.Core.Domain.Resource;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Domain.Model
{
    /// <summary>
    /// 每周菜肴
    /// </summary>
    public partial class WeekDish : IAggregationRoot
    {
        #region 构造函数

        internal WeekDish() {
            this.ID = IDHelper.CreateNewID();
        }

        #endregion

        #region 私有属性

        private IEntityRepository<WeekDish> _Repository;
        private IEntityRepository<WeekDish> Repository
        {
            get
            {
                if (_Repository == null)
                    _Repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<WeekDish>();

                return _Repository;
            }
        }

        #endregion

        #region 业务方法

        internal void Add()
        {
            Validate();

            this.CreateAudit = new Audit();
            this.LastUpdateAudit = new Audit();

            this.Repository.Add(this);
        }

        internal void Update()
        {
            Validate();

            this.LastUpdateAudit = new Audit();

            this.Repository.Update(this);
        }

        internal void AddItem(WeekDishItem item)
        {
            bool result = this.WeekDishItems.Where(p => p.DishID == item.DishID).Count() > 0;
            if (result)
                throw new BusinessValidationException(DishResource.ValidateException_WeekDishItem_Repeat);

            this.WeekDishItems.Add(item);
        }

        internal void ClearItems()
        {
            WeekDishItem[] array = this.WeekDishItems.ToArray();
            foreach (WeekDishItem item in array)
                item.Delete();

            this.WeekDishItems.Clear();
        }

        //internal void Delete()
        //{
        //    this.Repository.Delete(this);
        //}

        #endregion

        #region 验证方法

        protected void Validate()
        {
            List<string> messageList = new List<string>();

            ValidateTimeRepeat(messageList);
            //ValidateCodeRepeat(messageList);

            if (messageList.Count > 0)
                throw new BusinessValidationException(messageList.ToArray());
        }

        /// <summary>
        /// 验证指定时间段内是否有重复的每周菜单
        /// </summary>
        /// <param name="messageList"></param>
        private void ValidateTimeRepeat(IList<string> messageList)
        {
            ISpecification<WeekDish> spec = Specification<WeekDish>.Create(
                p => (p.BeginDate >= this.BeginDate && p.BeginDate <= this.EndDate)
                      ||
                      (p.EndDate >= this.BeginDate && p.EndDate <= this.EndDate)                 
                    );
            spec = spec.And(Specification<WeekDish>.Create(p => p.ID != ID));

            bool result = Repository.Count(spec) > 0;
            if (result)
                messageList.Add(string.Format(DishResource.ValidateException_WeekDishTime_Repeat));
        }

        //private void ValidateCodeRepeat(IList<string> messageList)
        //{
        //    ISpecification<WeekDish> spec = Specification<WeekDish>.Create(p => p.Code == this.Code);
        //    spec = spec.And(Specification<WeekDish>.Create(p => p.ID != ID));

        //    bool result = Repository.Count(spec) > 0;
        //    if (result)
        //        messageList.Add(string.Format(MaterialContractResource.ValidateException_ContractCode_Repeat, this.Code));
        //}

        #endregion
    }
}
